# This script converts a file of transcription factors from
# http://planttfdb.cbi.pku.edu.cn/download.php
# to a file that can be used as an annoDB for execAnno (or through runSingleTCW).
# All desired files need to be concatenated into TF.fasta.
#
# Change the %species hash to correspond to your set

# >Sbi000009|Sorghum_bicolor|LBD|Sb01g012810;XM_002466674
# changed to
# >TF|Sbi000009 Transcription factor (LBD|Sb01g012810;XM_002466674) OS=Sorghum Bicolor

use strict;
use warnings;
$| = 1;

my $input="TF.fasta";
my $output="plantTFDB.fasta";

# change this list to correspond to the species in your TF.fasta
# it works best in TCW if they are converted to the UniProt species name
my %species = ( 	
	"Arabidopsis_thaliana" => "Arabidopsis thaliana",
	"Zea_mays" => "Zea mays",
	"Oryza_sativa_subsp._indica" => "Oryza sativa subsp. indica",
	"Oryza_sativa_subsp._japonica" => "Oryza sativa subsp. japonica",
	"Sorghum_bicolor" => "Sorghum bicolor"
);

print "\n>>>Write transcription factor file: ", $output, "\n";
open IN, "<$input" or die "Error opening input: $input";
open OUT, ">$output" or die "Error opening output: $output";
my $cnt=0;

while(my $line = <IN>) {
	if ($line !~ />/) 
	{
		print OUT $line;
		next;
	}
	if ($line =~ /(\w+)\|([^|]+)\|(.*)$/) 
	{
		my $sp = $2;
		if (exists $species{$2}) {$sp = $species{$2};}
		else {
			print "$2: $line\n";
		}

		print OUT ">TF|$1 Transcription factor ($3) OS=$sp\n";
		$cnt++;
	}
	else {
		print $line;
	}
}
close OUT;
close IN;
print "\tWrote $cnt records\n";
